package GUI.Zonas;

import BE.BEZona;
import BL.BLCombo;
import BL.BLValidaciones;
import BL.BLZona;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;

public class GUIModificaZona extends javax.swing.JDialog {

    BEZona zona = null;
    /** Creates new form GUIModificaZona */
    public GUIModificaZona(java.awt.Frame parent, boolean modal, int idZona) {
        super(parent, modal);
        initComponents();
        inicializa();
        this.llenaVentana(idZona);
    }

    public void inicializa()
    {
        this.setLocationRelativeTo(null);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);
        
        BLCombo bLCombo = new BLCombo();
        bLCombo.setCombo("TIPOPRODUCTO",1,cmbTipo);
    }

    public void llenaVentana (int idProd)
    {
        BLZona blZona = new BLZona();
        zona = blZona.getZonabyId(idProd);

        if (zona != null)
        {
            txtNombre1.setText(zona.getNombre());
            txtarDescripcion1.setText(zona.getDescripcion());
            txtCant.setText(zona.getCantRack() + "");

            int i = 0;
            for (i=0;i<cmbTipo.getModel().getSize();i++)
            {
                BLCombo s = (BLCombo)cmbTipo.getModel().getElementAt(i);
                if (s.id == zona.getIdTipo())
                    break;
            }
            cmbTipo.setSelectedIndex(i);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        pnlDatosZona1 = new javax.swing.JPanel();
        lblNombre1 = new javax.swing.JLabel();
        lblAncho1 = new javax.swing.JLabel();
        txtNombre1 = new javax.swing.JTextField();
        txtCant = new javax.swing.JFormattedTextField();
        lblDescripcion1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtarDescripcion1 = new javax.swing.JTextArea();
        lblTipo = new javax.swing.JLabel();
        cmbTipo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Editar Zona");
        setName("GUIModificarZona"); // NOI18N

        btnAceptar.setText("Aceptar");
        btnAceptar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnAceptar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        pnlDatosZona1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Zona"));

        lblNombre1.setText("Nombre:");

        lblAncho1.setText("Cantidad de Rack:");

        txtCant.setEditable(false);

        lblDescripcion1.setText("Descripción:");

        txtarDescripcion1.setColumns(20);
        txtarDescripcion1.setRows(5);
        jScrollPane2.setViewportView(txtarDescripcion1);

        lblTipo.setText("Tipo:");

        javax.swing.GroupLayout pnlDatosZona1Layout = new javax.swing.GroupLayout(pnlDatosZona1);
        pnlDatosZona1.setLayout(pnlDatosZona1Layout);
        pnlDatosZona1Layout.setHorizontalGroup(
            pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosZona1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTipo)
                    .addComponent(lblNombre1)
                    .addComponent(lblAncho1)
                    .addComponent(lblDescripcion1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCant, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                    .addComponent(cmbTipo, 0, 148, Short.MAX_VALUE)
                    .addComponent(txtNombre1, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlDatosZona1Layout.setVerticalGroup(
            pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosZona1Layout.createSequentialGroup()
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombre1)
                    .addComponent(txtNombre1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAncho1)
                    .addComponent(txtCant, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTipo)
                    .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDescripcion1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlDatosZona1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlDatosZona1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        BLValidaciones validar = new BLValidaciones();
        int retorno;
        boolean cambio = false;
        BLZona blzona = new BLZona();
        BLCombo idTipo = (BLCombo)cmbTipo.getSelectedItem();

        if (!zona.getNombre().equals(txtNombre1.getText())) cambio = true;
        zona.setNombre(validar.validarSqlInjection(txtNombre1.getText()));
        zona.setDescripcion(validar.validarSqlInjection(txtarDescripcion1.getText()));
        zona.setIdTipo(idTipo.id);

        retorno = blzona.modificaZona(zona,cambio);
        if (retorno ==0)
        {
            JOptionPane.showMessageDialog(null,"Se ha editado la zona",
                                               "Información",JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GUIModificaZona dialog = new GUIModificaZona(new javax.swing.JFrame(), true,0);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cmbTipo;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAncho1;
    private javax.swing.JLabel lblDescripcion1;
    private javax.swing.JLabel lblNombre1;
    private javax.swing.JLabel lblTipo;
    private javax.swing.JPanel pnlDatosZona1;
    private javax.swing.JFormattedTextField txtCant;
    private javax.swing.JTextField txtNombre1;
    private javax.swing.JTextArea txtarDescripcion1;
    // End of variables declaration//GEN-END:variables

}
